/**
 * ManageAction.java 2015年12月21日 上午9:44:07
 *
 * Copyright (c) 2010-2061 leying.com, Inc. All rights reserved.
 *
 * @Description TODO
 * @version 3.0
 *
 */
package com.xiake.cms.web.action.back.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiake.core.entity.manager.Menu;
import com.xiake.core.entity.manager.MngUser;
import com.xiake.core.entity.manager.Role;
import com.xiake.core.model.ReturnDataModel;
import com.xiake.service.manager.ManageService;
import com.xiake.utils.PageInfo;
import com.xiake.utils.PageUtil;

@Controller
@RequestMapping(value = "back/manage")
public class ManageController {
  @Autowired
  private ManageService manageService;

  @RequestMapping(value = "mngUserList")
  public String mngUserList(ModelMap map, Integer roleId, String keyword,@RequestParam(defaultValue="1", required=false) Integer pageNo) throws Exception {
    PageInfo pageInfo = new PageInfo(pageNo, 20);
    List<MngUser> users = manageService.findMngUserList(roleId, keyword, pageInfo);
    Map<String, String> params = new HashMap<String, String>();
    params.put("roleId", (roleId == null || roleId.intValue() == 0)? "" : roleId.intValue()+"");
    params.put("keyword", keyword);
    List<Role> roles = manageService.findRoleList();
    map.put("roles", roles);
    String pagination = PageUtil.transToHtml("mngUserList", pageInfo , params);
    map.put("musers", users);
    map.put("pageInfo", pageInfo);
    map.put("roleId", roleId);
    map.put("keyword", keyword);
    map.put("pagination", pagination);
    return "back/manage/mngUserList";
  }
  
  @RequestMapping(value = "toSaveMngUser",method=RequestMethod.GET)
  public String toSaveMngUser(ModelMap map, Integer mngUserId) throws Exception {
    List<Role> roles = manageService.findRoleList();
    map.put("roles", roles);
    if(mngUserId != null && mngUserId.intValue() > 0){
    	MngUser muser = manageService.findMngUserById(mngUserId);
    	map.put("muser", muser);
    }
    return "back/manage/mngUserSave";
  }
  
  @RequestMapping(value = "saveMngUser",method=RequestMethod.POST)
  public String saveMngUser(ModelMap map, Integer id,String username,String truename,String password,Integer roleId) throws Exception {
	  manageService.saveMngUser(truename, username, password, roleId, id);
    return "redirect:mngUserList";
  }
  
  @RequestMapping(value="checkUsername")
  @ResponseBody
  public ReturnDataModel checkUsername(@RequestParam(required=true) String username) throws Exception{
	  boolean result = manageService.checkUsername(username);
	  ReturnDataModel rdm = new ReturnDataModel(result ? ReturnDataModel.RETURN_SUCCESS : ReturnDataModel.RETURN_FAILURE, "验证完毕");
	  return rdm;
  }
  @RequestMapping(value="menuList")
  public String menuList(ModelMap map,Integer roleId) throws Exception{
	  if(roleId != null && roleId.intValue() == 0){
		  roleId = null; 
	  }
	  List<Menu> menus = manageService.findMenuByRoleId(roleId);
	  List<Role> roles = manageService.findRoleList();
	  map.put("roles", roles);
	  map.put("menus", menus);
	  map.put("roleId", roleId);
	  return "back/manage/menuList";
  }
  
  @RequestMapping(value="saveMenu")
  public String saveMenu(ModelMap map,Integer id,String name,String url,String icon,Integer parentId,Integer index) throws Exception{
	  manageService.saveMenu(id, name, url, icon, parentId, index);
	  return "redirect:menuList";
  }
  
  @RequestMapping(value="roleList")
  public String roleList(ModelMap map) throws Exception{
	  List<Role> roles = manageService.findRoleList();
	  map.put("roles", roles);
	  return "back/manage/roleList";
  }
  
  @RequestMapping(value="toSaveRole")
  public String toSaveRole(ModelMap map, Integer id) throws Exception{
	  Role role = manageService.findRoleById(id);
	  List<Menu> menus = manageService.findMenuByWithChecked(id);
	  map.put("role", role);
	  map.put("menus", menus);
	  return "back/manage/roleSave";
  }
  
  @RequestMapping(value="saveRole")
  public String saveRole(Integer id, String name, Integer[] menuids) throws Exception{
	  manageService.saveRole(id, name, menuids, 1);
	  return "redirect:roleList";
  }
  
  @RequestMapping(value="deleteRole")
  public String deleteRole(Integer id) throws Exception{
	  manageService.deleteRoleById(id);
	  return "redirect:roleList";
  }
  
  @RequestMapping(value="deleteMenu")
  public String deleteMenu(Integer id) throws Exception{
	  manageService.deleteMenuById(id);
	  return "redirect:menuList";
  }
  
  @RequestMapping(value="deleteMUser")
  public String deleteMUser(Integer id) throws Exception{
	  manageService.deleteMUserById(id);
	  return "redirect:mngUserList";
  }
}
